package com.hjc.service.impl;

import com.hjc.dto.AssignRoleDto;
import com.hjc.dto.RoleAuthDto;
import com.hjc.entity.Result;
import com.hjc.entity.Role;
import com.hjc.mapper.RoleMapper;
import com.hjc.page.Page;
import com.hjc.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

//指定缓存的名称(数据保存到redis中的键的前缀，一般是标注@CacheConfig注解的类的全路径)
// @CacheConfig(cacheNames = "com.hjc.service.impl.RoleServiceImpl")
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;

    //查询方法上标注@Cacheable指定缓存的键
    // @Cacheable(key = "'all:role'")
    @Override
    public List<Role> queryAllRole() {
        return roleMapper.findAllRole();
    }

    @Transactional
    @Override
    public void assignRole(AssignRoleDto assignRoleDto) {
        Integer userId = assignRoleDto.getUserId();
        List<String> roleCheckList = assignRoleDto.getRoleCheckList();
        roleMapper.deleteRoleByUserId(userId);
        for (String roleName : roleCheckList) {
            int roleId = roleMapper.findRoleIdByRoleName(roleName);
            int flag = roleMapper.insertUserRole(userId, roleId);
        }
    }

    @Override
    public Page queryRoleByPage(Page page, Role role) {
        Integer roleRowCount = roleMapper.findRoleRowCount(role);
        List<Role> roleList = roleMapper.findRoleByPage(page, role);
        page.setTotalNum(roleRowCount);
        page.setResultList(roleList);
        return page;
    }

    // @CacheEvict(key = "'all:role'")
    @Override
    public Result addRole(Role role) {
        int flag = roleMapper.selectRoleByNameOrCode(role.getRoleName(), role.getRoleCode());
        if (flag > 0) {
            return Result.err(Result.CODE_ERR_BUSINESS,"该角色已存在，添加失败！");
        }
        int conut = roleMapper.insertRole(role);
        if (conut > 0) {
            return Result.ok("添加角色成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"添加失败！");
    }

    @Override
    public Result setRoleState(Integer roleId, String roleState) {
        int flag = roleMapper.updateRoleState(roleId, roleState);
        if (flag > 0) {
            return Result.ok("跟新角色状态成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"更新失败！");
    }

    @Override
    public Result removeRoleByRoleId(Integer roleId) {
        int flag = roleMapper.deleteRoleByRoleId(roleId);
        if (flag > 0) {
            return Result.ok("删除成功！");
        }
        return Result.err(Result.CODE_ERR_BUSINESS,"删除失败！");
    }

    @Override
    public Result setRoleDescByRoleId(Role role) {
        int flag = roleMapper.updateRoleDescByRoleId(role);
        if (flag > 0) {
            return Result.ok("更新成功!");
        }
        return Result.err(Result.CODE_ERR_BUSINESS, "更新失败！");
    }

    @Override
    public List<Integer> findAuthIdListByRoleId(int roleId) {
        return roleMapper.selectAuthIdListByRoleId(roleId);
    }

    @Transactional
    @Override
    public void addRoleAuth(RoleAuthDto roleAuthDto) {
        Integer roleId = roleAuthDto.getRoleId();
        List<Integer> authIdList = roleAuthDto.getAuthIds();
        roleMapper.deleteRoleAuthByRoleId(roleId);
        for (Integer authId : authIdList) {
            roleMapper.insertRoleAuth(roleId,authId);
        }
    }
}
